$(document).ready(
		function() {
			$("form").submit(function() {
				return false;
			});
			var VAL = {
				NORMAL : "0000000",
				/*EVT_USER : $("#userId").text(),*/
				EVT_USER : "C1702003",
				T_FIPINQBOX: "FIPINQBOX",
				T_FIPINQSHT: "FIPINQSHT",
				T_FSPPRDSCRP : "FSPPRDSCRP",
				T_FBPLSTDAT : "FBPBISDAT",
				DISABLED_ATTR : {
					"disabled" : true
				},
				ENABLED_ATTR : {
					"disabled" : false
				}
			};
			var domObj = {
				$window : $(window),

				$prdSeqId : $("#prdSeqId"),
				$boxId: $("#boxId"),
				$boxIdI : $("#boxIdI"),
				$shtOpeMsg : $("#shtOpeMsg"),
				$prdSeqRadio : $("#prdSeqRadio"),
				$boxIdRadio : $("#boxIdRadio"),
				grids : {
					$prdInfoGrd : $("#prdInfoGrd"),
					$rsnInfoGrd : $("#rsnInfoGrd")
				},

				buttons : {
					$scrapped_btn : $("#scrapped_btn"),
					$cancel_btn : $("#cancel_btn"),
					$clean_btn : $("#clean_btn"),
					$addRsn_btn : $("#addRsn_btn"),
					$delRsn_btn : $("#delRsn_btn"),
                    $delete_btn  : $("#delete_btn")
                },
				
				dialogs : {
					$addReasonDialog : $("#addReasonDialog"),
	                $deptId : $("#deptId"),
	                $modalReasonListDiv : $("#modalReasonListDiv"),
	                $modalReasonListGrd : $("#modalReasonListGrd"),
	                $modalReasonListPg : $("#modalReasonListPg"),

	                $reModalReasonListDiv : $("#reModalReasonListDiv"),
	                $reModalReasonListGrd : $("#reModalReasonListGrd"),
	                $reModalReasonListPg : $("#reModalReasonListPg"),

	                // 对话框的确认按钮
	                $reasonDialog_sureBtn : $("#reasonDialog_sureBtn"),
				}

			};

			function initPrdInfoGrd() {
				domObj.grids.$prdInfoGrd.jqGrid({
					url : "",
					datatype : "local",
					mtype : "POST",
					height : 400,
					width : $("#prdInfoGrdDiv").width() * 0.99,
		            autowidth: true,
		            shrinkToFit: true,
					resizable : true,
					loadonce : true,
					viewrecords : true,
					rownumbers : true,
					rownumWidth : 30,
					rowNum : 20,
					emptyrecords : true,
					//multiselect : true,
					colModel : [ {
						name : 'prd_seq_id',
						index : 'prd_seq_id',
						label : '玻璃/面板代码',
						width : 150
					}, {
						name : 'wo_id_fk',
						index : 'wo_id_fk',
						label : '工单',
						width : 150
					}, {
						name : 'mdl_id_fk',
						index : 'mdl_id_fk',
						label : '产品名称',
						width : 150
					}, {
						name : 'prd_stat',
						index : 'prd_stat',
						label : '玻璃/面板状态',
						width : 149
					}, {
						name : 'pv_prd_stat_fk',
						index : 'pv_prd_stat_fk',
						label : '玻璃/面板上一状态',
						width : 149	
					}, {
						name : 'cr_ope_no_fk',
						index : 'cr_ope_no_fk',
						label : '当前站点',
						width : 150
					}, {
						name : 'sht_ope_msg',
						index : 'sht_ope_msg',
						label : '备注信息',
						width : 250
					}, {
						name : 'evt_usr',
						index : 'evt_usr',
						label : '操作用户',
						width : 150
					},
					{
						name : 'evt_timestamp',
						index : 'evt_timestamp',
						label : '操作时间',
						width : 150
					}, 
					],
					pager : "prdInfoPg"
//						,
//					onSelectRow : function(rowId) {
//						var selRowId, rowData, iary, woId, inObj, outObj, oary;
//						rowData = domObj.grids.$prdInfoGrd.jqGrid("getRowData",
//								rowId);
//						var pvPrdStatFk = rowData.pv_prd_stat_fk;
//						var prdStat = rowData.prd_stat;
//						if(pvPrdStatFk == "WAIT"){
//							domObj.$boxIdI.attr(VAL.ENABLED_ATTR);
//						}else{
//							domObj.$boxIdI.attr(VAL.DISABLED_ATTR);
//						}
//						domObj.$prdSeqId.val(rowData.prd_seq_id);
//						domObj.$shtOpeMsg.val(rowData.sht_ope_msg);
//					}
				});
			}
			
			
			function initRsnInfoGrd(){
				domObj.grids.$rsnInfoGrd.jqGrid({
	                url : "",
	                datatype : "local",
	                mtype : "POST",
	                height : 150,
	                width : $("#rsnInfoGrdDiv").width(),
	                autowidth : false,
	                shrinkToFit : true,
	                resizable : true,
	                loadonce : true,
	                viewrecords : true,
	                rownumbers : true,
	                rowNum : 20,
	                emptyrecords : true,
	                multiselect : false,
	                pager :  $("#rsnInfoPg"),
	                colModel : [ {
                        name : 'dept_id',
                        index : 'dept_id',
                        label : '部门代码',
                        width : 100
                    }, {
                        name : 'data_ext',
                        index : 'data_ext',
                        label : '原因代码',
                        width : 150
                    }, {
                        name : 'data_desc',
                        index : 'data_desc',
                        label : '原因代码说明',
                        width : 180
                    }]
	            });
			};

			 var inModelGrid = function() {
		            domObj.dialogs.$modalReasonListGrd
		                .jqGrid("clearGridData");
		            domObj.dialogs.$reModalReasonListGrd
		                .jqGrid("clearGridData");
		            domObj.dialogs.$modalReasonListGrd
	                .jqGrid({
	                    url : "",
	                    datatype : "local",
	                    mtype : "POST",
	                    height : 300,
	                    // width :
	                    // domObj.dialogs.$modalReasonListDiv.width()*0.99,
	                    width : 400,
	                    autowidth : false,
	                    shrinkToFit : false,
	                    resizable : true,
	                    loadonce : true,
	                    viewrecords : true,
	                    rownumbers : true,
	                    rowNum : 20,
	                    emptyrecords : true,
	                    multiselect : false,
	                    pager : domObj.dialogs.$modalReasonListPg,
	                    colModel : [ {
	                        name : 'data_ext',
	                        index : 'data_ext',
	                        label : '原因代码',
	                        width : 150
	                    }, {
	                        name : 'data_desc',
	                        index : 'data_desc',
	                        label : '原因代码说明',
	                        width : 180
	                    } ],
	                    onSelectRow : function(rowId, status) {
							var dept_id = domObj.dialogs.$deptId.val();
							if(!dept_id){
                                showErrorDialog("", "请先选择部门代码！");
                                return false;
							}

	                        var rowId= domObj.dialogs.$modalReasonListGrd
	                            .jqGrid("getGridParam", "selrow");
	                        var rowData = domObj.dialogs.$modalReasonListGrd.jqGrid(
	                            "getRowData", rowId);
	                        var rsn_data={
	                        	dept_id  : dept_id,
								data_ext : rowData.data_ext,
								data_desc  : rowData.data_desc
							}
	                        setGridInfo(rsn_data, "#reModalReasonListGrd");
	                    }
	                });

	            domObj.dialogs.$reModalReasonListGrd.jqGrid({
	                url : "",
	                datatype : "local",
	                mtype : "POST",
	                height : 400,
	                // width:domObj.dialogs.$reModalReasonListDiv.width()*
	                // 0.99,
	                width : 400,
	                autowidth : false,
	                shrinkToFit : false,
	                resizable : true,
	                loadonce : true,
	                viewrecords : true,
	                rownumbers : true,
	                rowNum : 20,
	                emptyrecords : true,
	                multiselect  : false,
	                pager : domObj.dialogs.$reModalReasonListPg,
	                colModel : [ {
	                    name : 'dept_id',
	                    index : 'dept_id',
	                    label : '部门代码',
	                    width : 100
	                }, {
	                    name : 'data_ext',
	                    index : 'data_ext',
	                    label : '原因代码',
	                    width : 100
	                }, {
	                    name : 'data_desc',
	                    index : 'data_desc',
	                    label : '原因代码说明',
	                    width : 100
	                } ]
	            });
			 };
			 
			 var dialogFnc = {
					 reasonSureFnc : function() {
			                var Data = domObj.dialogs.$reModalReasonListGrd.jqGrid("getRowData");
			                if(Data.length<1){
			                    showErrorDialog("", "请选择一条原因代码");
			                    return false;
			                }
			                setGridInfo(Data, "#rsnInfoGrd");
			                domObj.dialogs.$addReasonDialog.modal("hide");
			            }
			 }
			
			//radio
			  domObj.$prdSeqRadio.click(function(){
				    //alert(this.checked);
				    var $radio = $(this);
				    // if this was previously checked
					$radio.prop('checked', true);
					domObj.$boxIdRadio.prop('checked', false);
					domObj.$boxId.attr(VAL.DISABLED_ATTR);
					domObj.$prdSeqId.attr(VAL.ENABLED_ATTR);
					domObj.$prdSeqId.val("");
					domObj.$prdSeqId.focus();
					domObj.$boxId.val("");
				  });
			
			domObj.$boxIdRadio.click(function(){
				    var $radio = $(this);
					$radio.prop('checked', true);
					domObj.$prdSeqRadio.prop('checked', false);
					domObj.$boxId.attr(VAL.ENABLED_ATTR);
					domObj.$prdSeqId.attr(VAL.DISABLED_ATTR);
					domObj.$prdSeqId.val("");
					domObj.$boxId.val("");
					domObj.$boxId.focus();
			});
			
			
			var buttonFnc = {
				scrappedFnc : function (){
					var prdSeqId,boxId,shtOpeMsg,rsnId,iary,rowData,inObj,outObj;
					var iary = new Array();
					boxId = domObj.$boxId.val().trim();
					shtOpeMsg = domObj.$shtOpeMsg.val();
					var rowDatas = $("#prdInfoGrd").jqGrid("getRowData");
					if(rowDatas.length ==0){
						showErrorDialog("", "请选择需要报废的玻璃！");
						return false;
					}
					for(var i=0;i<rowDatas.length;i++){
						var prd_id={
							prd_seq_id : rowDatas[i].prd_seq_id
						}
						iary.push(prd_id);
						if(rowDatas[i].prd_stat.trim() == "SCRP"){
							showErrorDialog("", "玻璃["+rowDatas[i].prd_seq_id+"]已经报废过了，无需再次报废");
							return false;
						}
					}
					
					var rowIds = domObj.grids.$rsnInfoGrd.jqGrid("getDataIDs");
					if(rowIds.length == 0){
						showErrorDialog("", "请选择报废部门及原因代码！");
						return false;
					}

					if(!shtOpeMsg){
						showErrorDialog("", "备注信息不能为空！");
						return false;
					}
					
					rowData = domObj.grids.$rsnInfoGrd.jqGrid("getRowData",rowIds[0]);
					
					var rsn_dept = rowData.dept_id;
					var rsn_code = rowData.data_ext;
					var dataDesc = rowData.data_desc;
					inObj = {
							trx_id : VAL.T_FSPPRDSCRP,
							iary : iary,
							box_id : boxId,
							sht_ope_msg : shtOpeMsg,
							rsn_dept : rsn_dept,
							rsn_cate : "SCRS",
							rsn_code : rsn_code,
							evt_usr : VAL.EVT_USER
					};
					if(domObj.$prdSeqRadio.prop("checked") == true && rowDatas[0].prd_stat.trim() == "INPR"){
						inObj.action_flg = "S"
					}else if(domObj.$prdSeqRadio.prop("checked") == true && rowDatas[0].prd_stat.trim() != "INPR"){
						showErrorDialog("","单片玻璃报废，必须是INPR状态");
						return false;
                    }else{
						inObj.action_flg = "B"
					}
					outObj = comTrxSubSendPostJson(inObj);
					if(outObj.rtn_code === VAL.NORMAL){
						domObj.grids.$prdInfoGrd.jqGrid('clearGridData');
						showSuccessDialog("报废成功！");
					}
				}, 
				
				cancelFnc  : function() {
					var shtOpeMsg ,id, inObj, outObj,rowData,iary;
					shtOpeMsg = domObj.$shtOpeMsg.val();
					var iary = new Array();
					var rowDatas = $("#prdInfoGrd").jqGrid("getRowData");
					if(rowDatas.length ==0){
						showErrorDialog("", "请选择需要取消报废的玻璃！");
						return false;
					}
					for(var i=0;i<rowDatas.length;i++){
						if(rowDatas[i].prd_stat.trim() != "SCRP"){
							showErrorDialog("", "玻璃["+rowDatas[i].prd_seq_id+"]未报废，无需取消报废！");
							return false;
						}
						if(rowDatas[i].pv_prd_stat_fk.trim() == "INPR"){
                            showErrorDialog("", "玻璃["+rowDatas[i].prd_seq_id+"]上次状态为INPR，不可取消报废！");
                            return false;
						}
						var prd_id={
							prd_seq_id : rowDatas[i].prd_seq_id
						}
						iary.push(prd_id);
					}
					
					var pvPrdStatFk = rowDatas[0].pv_prd_stat_fk;
					var boxIdI = domObj.$boxIdI.val().trim();
					if (pvPrdStatFk == 'WAIT'){
						if(boxIdI == ""){
							showErrorDialog("", "玻璃上次状态为WAIT，请输入转入卡匣再取消报废！");
							return false;
						}
					}
					
//					var rowIds = domObj.grids.$rsnInfoGrd.jqGrid("getDataIDs");
//					if(rowIds.length != 0){
//						showErrorDialog("", "取消报废不需要原因代码，请删除原因代码！");
//						return false;
//					}
//					
					if(!shtOpeMsg){
						showErrorDialog("", "备注信息不能为空！");
						return false;
					}
					
					inObj = {
							trx_id : VAL.T_FSPPRDSCRP,
							action_flg : 'C',
							iary : iary,
							box_id_cancel_in :  boxIdI,
							sht_ope_msg : shtOpeMsg,
							evt_usr : VAL.EVT_USER
					};
					outObj = comTrxSubSendPostJson(inObj);
					if(outObj.rtn_code === VAL.NORMAL){
						showSuccessDialog("取消报废成功！");
					}
				},

				cleanFnc : function() {
					domObj.$boxId.val("");
					domObj.$boxIdI.val("");
					domObj.$prdSeqId.val("");
					domObj.$shtOpeMsg.val("");
					domObj.$prdSeqRadio.prop('checked',false);
					domObj.$boxIdRadio.prop('checked',false);
					domObj.$prdSeqId.attr(VAL.DISABLED_ATTR);
					domObj.$boxId.attr(VAL.DISABLED_ATTR);
//					domObj.$boxIdI.attr(VAL.DISABLED_ATTR);
					domObj.grids.$prdInfoGrd.jqGrid('clearGridData');
					domObj.grids.$rsnInfoGrd.jqGrid('clearGridData');
					$("#modalReasonListDiv").jqGrid('clearGridData');
			        $("#reModalReasonListDiv").jqGrid('clearGridData');
			        
				},
				
				 selectReasonBtnfunc : function() {
						var rowCnt = domObj.grids.$prdInfoGrd.getGridParam("reccount");
						if(rowCnt == 0){
                            showErrorDialog("", "请先获取需要报废的玻璃信息，再选择原因代码！");
                            return false;
						}

		                domObj.dialogs.$modalReasonListGrd
		                    .jqGrid("clearGridData");
		                domObj.dialogs.$reModalReasonListGrd
		                    .jqGrid("clearGridData");
		                // 原因代码对话框的模型
		                domObj.dialogs.$addReasonDialog.modal({
		                    backdrop : true,
		                    keyboard : false,
		                    show : false
		                });
						//初始化部门代码
                     	comAddValueByDataCateFnc("#deptId","DEPT","data_ext","data_ext",true);

                        //初始化报废原因代码
						 var iary, inObj, outObj;
						 deptId = domObj.dialogs.$deptId.val();
						 iary = {
							 data_cate : "SCRS"
						 };
						 inObj = {
							 trx_id : VAL.T_FBPLSTDAT,
							 action_flg : "Q",
							 iary : [ iary ]
						 };
						 outObj = comTrxSubSendPostJson(inObj);
						 if (outObj.rtn_code === VAL.NORMAL) {
							 var deptIds = domObj.dialogs.$deptId.val();
							 setGridInfo(outObj.oary,"#modalReasonListGrd");
                             domObj.dialogs.$addReasonDialog.modal("show");
						 }
		            },
		            deleteReasonBtnfunc : function() {
		                var selRowId = domObj.grids.$rsnInfoGrd.jqGrid(
		                    "getGridParam", "selrow");
		                if (selRowId==null||selRowId.length < 1) {
		                    showErrorDialog("", "请至少选择一条原因代码进行删除");
		                    return false;
		                }
		                $("#deleteReason_btn").showCallBackWarnningDialog(
		                    {
		                        errMsg : "是否删选原因代码,请确认!!!!",
		                        callbackFn : function(data) {
		                            if (data.result == true) {
		                                // 动态删除jqgrid行时，数据会在动态变化
		                                var length = selRowId.length;
		                                for (var i = 0; i < length; i++) {
		                                    domObj.grids.$rsnInfoGrd.jqGrid("delRowData",selRowId[0]);
		                                }
		                            }
		                        }
		                    });
		            }

			};

			domObj.buttons.$scrapped_btn.bind('click', buttonFnc.scrappedFnc);
			domObj.buttons.$cancel_btn.bind('click', buttonFnc.cancelFnc);
			domObj.buttons.$clean_btn.bind('click', buttonFnc.cleanFnc);
			domObj.buttons.$addRsn_btn.bind('click',buttonFnc.selectReasonBtnfunc);
			domObj.buttons.$delRsn_btn.bind('click',buttonFnc.deleteReasonBtnfunc);
			domObj.dialogs.$reasonDialog_sureBtn.bind('click',dialogFnc.reasonSureFnc);
			
			domObj.$boxId.keydown(function(event) {
				var inObj, outObj, tblCnt, boxId, newRowId, iary;
				if (event.keyCode == 13) {
					boxId = domObj.$boxId.val();
					domObj.$boxIdI.val("");
					inObj = {
						trx_id : VAL.T_FIPINQBOX,
						action_flg : 'Q',
						box_id : boxId
					}, 
					outObj = comTrxSubSendPostJson(inObj);
					if (outObj.rtn_code === VAL.NORMAL) {
						if(outObj.box_stat.trim() != "WAIT"){
							showErrorDialog("", "整卡匣报废时，箱子的状态必须是[WAIT]！");
		                    return false;
						}
						
						domObj.$shtOpeMsg.val("");
						if(outObj.oary[0].prd_stat.trim() != "WAIT"){
							showErrorDialog("", "整卡匣报废时，必须是[WAIT]状态");
		                    return false;
						}
						setGridInfo(outObj.oary, "#prdInfoGrd", true);
					}
				}
			});

			domObj.$prdSeqId.keydown(function(event) {
				var inObj, outObj, tblCnt, prdSeqId, newRowId, iary;
				if (event.keyCode == 13) {
					prdSeqId = domObj.$prdSeqId.val();
					domObj.$boxIdI.val("");
					var rowDatas = $("#prdInfoGrd").jqGrid("getRowData");
					if(rowDatas.length>0){
						for (var i = 0; i < rowDatas.length; i++) {
							if (rowDatas[i].prd_seq_id.trim() == prdSeqId.trim()) {
								domObj.$prdSeqId.val("");
								return;
							}
						}
					}
					inObj = {
						trx_id : VAL.T_FIPINQSHT,
						action_flg : 'Q',
						prd_seq_id : prdSeqId
					}, 
					outObj = comTrxSubSendPostJson(inObj);
					if (outObj.rtn_code === VAL.NORMAL) {
						if(outObj.prd_stat != "INPR" && outObj.prd_stat != "SCRP" && outObj.prd_stat != "WAIT"){
							showErrorDialog("", "单片报废时，必须是[INPR]状态和[WAIT]状态;取消报废时，状态必须是[SCRP]");
		                    return false;
						}
						if(rowDatas.length>0){
							if (rowDatas[0].prd_stat.trim() != outObj.prd_stat.trim()) {
								showErrorDialog("", "待报废玻璃与已报废玻璃，不可同时操作！");
			                    return false;
							}
						}
						
						var newRowID = getGridNewRowID("#prdInfoGrd");
						$("#prdInfoGrd").jqGrid("addRowData",newRowID,outObj);
						domObj.$prdSeqId.val("");
						domObj.$prdSeqId.focus();
						
					}
				}
			});
			domObj.buttons.$delete_btn.click(function () {
                if(domObj.$boxIdRadio.prop("checked") == true){
                    showErrorDialog("", "按照卡匣进行报废时，不可以做删除操作！");
                    return false;
                }else if(domObj.$prdSeqRadio.prop("checked") == true){
                    var rowid = domObj.grids.$prdInfoGrd.jqGrid('getGridParam','selrow');
                    if(rowid == null){
                        showErrorDialog("", "请选择需要删除的玻璃代码！");
                        return false;
                    }
                    domObj.grids.$prdInfoGrd.jqGrid('delRowData', rowid);
                }
            })
			function initPage(){
				domObj.$boxId.val("");
				domObj.$boxIdI.val("");
				domObj.$prdSeqId.val("");
				domObj.$shtOpeMsg.val("");
				domObj.$prdSeqRadio.prop('checked',false);
				domObj.$boxIdRadio.prop('checked',false);
				domObj.$prdSeqId.attr(VAL.DISABLED_ATTR);
				domObj.$boxId.attr(VAL.DISABLED_ATTR);
//				domObj.$boxIdI.attr(VAL.DISABLED_ATTR);
				domObj.grids.$prdInfoGrd.jqGrid('clearGridData');
				$("#modalReasonListDiv").jqGrid('clearGridData');
		        $("#reModalReasonListDiv").jqGrid('clearGridData');
		        initPrdInfoGrd();
		        initRsnInfoGrd();
		        inModelGrid();
			};
			
			initPage();
            //表格自适应
            function resizeFnc(){
                var offsetBottom, divWidth;
                divWidth = $(".UnderDiv").width();
                offsetBottom =  $(window).height() - $("#prdInfoGrdDiv").offset().top;
                $("#prdInfoGrdDiv").height(offsetBottom * 0.95);
                $("#prdInfoGrd").setGridWidth(divWidth * 0.99);
                $("#prdInfoGrd").setGridHeight(offsetBottom * 0.99 -51);

                divWidth = $("#rsnInfoGrdDiv").width();
                offsetBottom = $("#rsnInfoGrdDiv").offset().top;
                $("#rsnInfoGrdDiv").height(offsetBottom * 0.95);
                $("#rsnInfoGrd").setGridWidth(divWidth * 0.99);
                $("#rsnInfoGrd").setGridHeight(offsetBottom * 0.99);
            };
            resizeFnc();
            $(window).resize(function() {
                resizeFnc();
            });
		})